home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 015 / hpstuff.arc / CPAINT.C next >
Text File  |  1987-02-11  |  9KB  |  210 lines

  1. /*===========================================================================*\
  2. ||  CPAINT  (See Paint)                    Rip Toren         ||
  3. ||                               POB 674         ||
  4. ||                               Columbia, MD 21045||
  5. ||                                         ||
  6. ||  Translate a file created by Microsoft (Zsoft) PcPaint into binary         ||
  7. ||     image files. These files contain HP LaserJet commands for start         ||
  8. ||     and stop of graphics.                             ||
  9. ||                                         ||
  10. ||  input : argv[1] = file name to process                     ||
  11. ||        argv[2] = flags of 'Y' and 'N' indicating which planes to        ||
  12. ||              produce output for. Up to 8 planes are handled, but    ||
  13. ||              EGA image has only 4.                     ||
  14. ||                                         ||
  15. || NOTE ! with 'C' opening 5 standard file                                   ||
  16. ||            +  1 input file                      ||
  17. ||            +  8 possible planes  (4 realisticaly )          ||
  18. ||            +  1 general purpose file                 ||
  19. ||            = 15 possible files  in config.sys             ||
  20. ||                                         ||
  21. \*===========================================================================*/
  22. #include "stdio.h"
  23. #include "stdlib.h"
  24.  
  25. typedef unsigned char byte;
  26. #define min(a,b) ((a)<=(b)?(a):(b))
  27.  
  28.        /*--------------------------------------------------------------------*\
  29.        |   Structure definition for first 128 bytes of .PCC or .PCX file      |
  30.        \*--------------------------------------------------------------------*/
  31. struct header {
  32.        byte    manuf;               /* manufacturer                 */
  33.        byte    version;            /* version number             */
  34.        byte    rllenc;               /* run-length packing turned on         */
  35.        byte    bits_p_pixel;           /* bit per pixel              */
  36.        int     x1,y1,x2,y2;           /* cooridinated to top/left bot/rite  */
  37.        int     horiz_res, vert_res;    /* resolution of producing screen     */
  38.        byte    palette[48];           /* beats me ???                 */
  39.        byte    vmode;
  40.        byte    planes;               /* number of planes in picture         */
  41.        int     bytes_p_line;           /* packed bytes per scan line         */
  42.        byte    filler [200];
  43.        };
  44.  
  45. FILE *mppic,*mpres;               /* input and output files         */
  46. FILE *res[8];                   /* a file for each possible plane     */
  47. char buff [512];
  48.  
  49.            /*      0   1    2    3    4      5    6    7     */
  50. static char mask[8]  ={ 0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe};
  51.                        /* names of potential outputs         */
  52. static char *dfname[] = {"plane1.dat","plane2.dat","plane3.dat","plane4.dat",
  53.              "plane5.dat","plane6.dat","plane7.dat","plane8.dat"};
  54. static char null_f[] = "NUL";          /* name for non-used planes           */
  55.  
  56.  
  57. /*---------------------------------------------------------------------------*\
  58. |  **************************** M A I N **********************************    |
  59. \*---------------------------------------------------------------------------*/
  60. main (argc,argv)
  61. int argc;
  62. char *argv[];
  63.   {
  64.    struct header *mp_hdr;
  65.    unsigned char *b, c, *fb;
  66.    int i, j, k, chr, rows,plane,bytes,bits, found_non_blank;
  67.    long byte_cnt;
  68.    char byte_l[5];
  69.  
  70.    if (argc < 2)  {
  71.        puts ("I can't read minds, how about a file name!");
  72.        exit(1);
  73.        }
  74.    /*------------------------------------------------------------------------*\
  75.    |   open input file                                  |
  76.    \*------------------------------------------------------------------------*/
  77.    mppic = fopen (argv[1],"rb");
  78.    if (mppic==NULL)  {
  79.        puts ("open failed on pic file ");
  80.        exit (1);
  81.        }
  82.    puts ("Open good");
  83.  
  84.    /*------------------------------------------------------------------------*\
  85.    |   Check for exclusion/inclusion of planes for output              |
  86.    \*------------------------------------------------------------------------*/
  87.    if (argc >1)                /* look for yes/no flags          */
  88.        {
  89.        strupr (argv[2]);           /* uppercase                 */
  90.        for (i=0; *argv[2] && i<8; i++,argv[2]++)
  91.        {
  92.        if (*argv[2] != 'Y') dfname[i] = null_f;
  93.        }
  94.        }
  95.  
  96.    /*------------------------------------------------------------------------*\
  97.    |   get and display file header                          |
  98.    \*------------------------------------------------------------------------*/
  99.    fread (buff, 1, 128, mppic);
  100.    mp_hdr = (struct header *) buff;
  101.    printf ("Manufacturer      = %d\n",(int) mp_hdr -> manuf            );
  102.    printf ("Version is        = %d\n",(int) mp_hdr -> version          );
  103.    printf ("Run length is     = %d\n",(int) mp_hdr -> rllenc           );
  104.    printf ("Bits / pixel      = %d\n",(int) mp_hdr -> bits_p_pixel     );
  105.    printf ("Coordinate x1     = %d\n",(int) mp_hdr -> x1               );
  106.    printf ("Coordinate y1     = %d\n",(int) mp_hdr -> y1               );
  107.    printf ("Coordinate x2     = %d\n",(int) mp_hdr -> x2               );
  108.    printf ("Coordinate y2     = %d\n",(int) mp_hdr -> y2               );
  109.    printf ("Horizontal res    = %d\n",(int) mp_hdr -> horiz_res        );
  110.    printf ("Vertical   res    = %d\n",(int) mp_hdr -> vert_res         );
  111.    printf ("Planes            = %d\n",(int) mp_hdr -> planes           );
  112.    printf ("Bytes / line      = %d\n",(int) mp_hdr -> bytes_p_line     );
  113.  
  114.                        /* allocate space for one scan line   */
  115.    fb = malloc (mp_hdr -> bytes_p_line);
  116.  
  117.    /*------------------------------------------------------------------------*\
  118.    |   Prime each of the files                              |
  119.    \*------------------------------------------------------------------------*/
  120.    for (i=0; i<mp_hdr -> planes; i++)
  121.        {
  122.        res[i] = fopen (dfname[i],"wb");
  123.                        /* set graphic for self origin         */
  124.        fprintf(res[i],"%c%s",0x1b,"*r1A");
  125.        }
  126.  
  127.    byte_cnt = 0;
  128.    found_non_blank = 0;
  129.                        /* bits per row                 */
  130.    bits = (mp_hdr -> x2 - mp_hdr -> x1) + 1;
  131.                        /* row in image                 */
  132.    rows = (mp_hdr -> y2 - mp_hdr -> y1) + 1;
  133.                        /* number of bytes in row (packed)    */
  134.    stci_d(byte_l, mp_hdr -> bytes_p_line);
  135.  
  136.    for (i=0; i<rows; i++)  {
  137.        for (plane=0; plane < mp_hdr -> planes; plane++)  {
  138.        mpres = res[plane];           /* select the file for output         */
  139.        k = bits;
  140.        for (bytes=0; bytes < mp_hdr -> bytes_p_line; bytes++)  {
  141.            if (EOF != encget (&chr, mppic))  {
  142.            byte_cnt ++;
  143.            c = chr ^ 0xff;     /* invert bits for HPLJ             */
  144.                        /* clear last few bits if not in image*/
  145.            if (k<8) c &= mask[k];
  146.            *(fb+bytes) = c;    /* store in save array             */
  147.                        /* indicate a non-blank row         */
  148.            if (c!='\0') found_non_blank = 1;
  149.            k -= 8;           /* decrem bit count             */
  150.            if (k<0 ) k=0;
  151.            }
  152.            }
  153.        if (found_non_blank)
  154.            {
  155.            fprintf(mpres,"%c%s%s%s",0x1b,"*b",byte_l,"W");
  156.            for (j=0;j< mp_hdr -> bytes_p_line;j++) fputc (*(fb+j),mpres);
  157.            }
  158.        }
  159.        }
  160.    printf (" BYTE COUNT       = %ld\n",byte_cnt);
  161.  
  162.    for (i=0; i<4; i++)    {
  163.        fprintf(res[i],"%c%s",0x1b,"*rB");
  164.        fclose (res[i]);
  165.        }
  166.    fclose (mppic);
  167.    }
  168.  
  169. /*===========================================================================*\
  170. || Get the bytes from the file.  If bits 6 & 7 are set, this is a count      ||
  171. || of repetitions of following byte.                         ||
  172. ||                                         ||
  173. \*===========================================================================*/
  174. encget (pbyt, fid)
  175. int *pbyt;
  176. FILE *fid;
  177.   {
  178.   static int cnt=0, i;
  179.    if (cnt > 0)  {               /* return the same byte again         */
  180.        cnt --;
  181.        *pbyt = i;
  182.        return (0);
  183.        }
  184.  
  185.                        /* no more characters             */
  186.    if (EOF== (i=fgetc(fid))) return (EOF);
  187.  
  188.    if (0xC0 == (0xC0 & i))  {           /* it is a repeat count             */
  189.        cnt = (0x3F & i)-1;
  190.        if (EOF == (i= fgetc(fid))) return (EOF);
  191.        }
  192.    *pbyt = i;                   /* return the byte read             */
  193.    return (0);
  194.    }
  195.  
  196.  
  197. /*===========================================================================*\
  198. || trash lines                                     ||
  199. || These lines were moved out , but saved for future use,             ||
  200. \*===========================================================================*/
  201.     /*-------------------------------------------------------------------*\
  202.     |  inner loop to produce character files, rather than binary          |
  203.     \*-------------------------------------------------------------------*/
  204.  /*           fputc (c,mpres);
  205.            for (j=0; j<min(8,k); j++)
  206.               if (((c << j) & 0x80) == 0) fputc ('0',mpres);
  207.              else              fputc ('1',mpres);
  208.            k -= j;    */
  209.  /*       fprintf (mpres,"%s","~\r\n"); */
  210.